package com.bacel.nb.product.web.controller;


import com.bacel.nb.product.core.domain.Sysuser;
import com.bacel.nb.product.core.service.ISysuserService;
import com.bacel.nb.product.web.template.ViewModel.FreeMarkerViewSimple;
import com.bacel.nb.product.web.util.MD5ArithmeticUtils;
import com.bacel.nb.product.web.util.VerifyCodeUtils;
import com.bacel.nb.product.web.util.WebUtilify;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Enumeration;
import java.util.List;

/**
 * Created by Bacel on 2017/4/18.
 */
@RestController
@RequestMapping(value = "/sysmanage", produces = "text/html;charset=UTF-8")
public class SysManageController {
    @Autowired
    private ISysuserService sysuserService;

    @RequestMapping(value = "/LoginOut", method = RequestMethod.GET)
    public void LoginOut(final HttpSession session, final HttpServletResponse response)
            throws Exception {
        Enumeration em = session.getAttributeNames();
        while (em.hasMoreElements()) {
            session.removeAttribute(em.nextElement().toString());
        }
        response.sendRedirect("/sysmanage/login");
    }

    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public ModelAndView login(final HttpSession session, final HttpServletResponse response)
            throws Exception {
        // 显示首页
        FreeMarkerViewSimple mav = new FreeMarkerViewSimple("login", "用户登录");
        return mav;
    }

    @RequestMapping(value = "/userlogin", method = RequestMethod.POST)
    public String userlogin(final HttpSession session, final HttpServletRequest request, final HttpServletResponse response)
            throws Exception {
        String usercode = request.getParameter("usercode");
        String userpws = request.getParameter("pws");
        String vcode = request.getParameter("vcode").toLowerCase();
        String sessionvcode = "";
        String errstr = "";
        if (session.getAttribute("verCode") != null) {
            sessionvcode = session.getAttribute("verCode").toString().toLowerCase();
            if (!userpws.equals("") && sessionvcode.equals(vcode) && !sessionvcode.equals("")) {
                String ret = "";
                Sysuser sysuser = new Sysuser();
                sysuser.setName(usercode);
                sysuser.setPassword(MD5ArithmeticUtils.getMd5(userpws));
                List<Sysuser> list = sysuserService.findByExampleEq(sysuser);
                if (list.size() > 0) {
                    session.setAttribute(WebUtilify.LoginUserKey, list.get(0));
                    session.removeAttribute("verCode");
                    errstr = "ok";
                } else {
                    errstr = "帐号或密码有误";
                }
            } else {
                errstr = "验证码输入有误002";
            }
        } else {
            errstr = "验证码输入有误001";
        }
        if (errstr.equals("ok"))
            errstr = "";
        return WebUtilify.ReturnJsoninfo(errstr);
    }

    @RequestMapping(value = "/GetVerifyimg", method = RequestMethod.GET)
    public void GetVerifyimg(final HttpSession session, final HttpServletResponse response)
            throws Exception {
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        response.setContentType("image/jpeg");
        //生成随机字串
        String verifyCode = VerifyCodeUtils.generateVerifyCode(4);
        //存入会话session
        //删除以前的
        session.removeAttribute("verCode");
        session.setAttribute("verCode", verifyCode.toLowerCase());
        //生成图片
        int w = 100, h = 30;
        VerifyCodeUtils.outputImage(w, h, response.getOutputStream(), verifyCode);
    }
}
